package com.bbzn.device.client.service.emq.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.dataobject.Device;
import com.bbzn.device.client.dto.api.ResultResponse;
import com.bbzn.device.client.dto.api.position.GetPositionRequest;
import com.bbzn.device.client.dto.api.position.GetPositionResult;
import com.bbzn.device.client.dto.position.PositionDTO;
import com.bbzn.device.client.dto.position.PositionResult;
import com.bbzn.device.client.dto.position.WifiPosition;
import com.bbzn.device.client.service.DeviceService;
import com.bbzn.device.client.service.EmqStrategy;
import com.bbzn.device.client.service.GaodeMapApiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: jcnehuang
 * @Date : 2021/4/9 11:05
 * @Version 1.0
 * WIFI定位
 */
@Slf4j
@Component
public class EmqWifiPositionStrategy implements EmqStrategy {

    @Autowired
    DeviceService deviceService;

    @Autowired
    GaodeMapApiService gaodeMapApiService;

    @Override
    public String emqAction(String payLoad) {
        PositionResult result = new PositionResult("3017", "", 0);
        try {
            PositionDTO positionDTO = JSONObject.parseObject(payLoad, PositionDTO.class);
            if (null == positionDTO || StringUtils.isEmpty(positionDTO.getImei()) || CollectionUtils.isEmpty(positionDTO.getWifis())) {
                result.setStatus(1);
                return JSON.toJSONString(result);
            }
            result.setImei(positionDTO.getImei());
            GetPositionRequest request = new GetPositionRequest();
            request.setImei(positionDTO.getImei());
            request.setOutput("json");
            request.setAccesstype(1);
            String macInfo = positionDTO.getWifis().stream().map(this::getMacInfo).filter(Objects::nonNull).collect(Collectors.joining("|", "", ""));
            request.setMacs(macInfo);
            request.setMmac(macInfo);
            ResultResponse<GetPositionResult> response = gaodeMapApiService.getPosition(request);
            if (null != response.getData() && response.getData().getStatus().equals("1") &&
                    StringUtils.isNotEmpty(response.getData().getResult().getLocation()) &&
                    StringUtils.isNotEmpty(response.getData().getResult().getDesc())) {
                Device device = new Device();
                device.setImei(positionDTO.getImei());
                device.setLocation(response.getData().getResult().getDesc());
                String lon = response.getData().getResult().getLocation().split(",")[0];
                String lat = response.getData().getResult().getLocation().split(",")[1];
                device.setLon(lon);
                device.setLat(lat);
                // 设置成功
                deviceService.updateByImeiSelective(device);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "WIFI定位失败！";
        }
        return JSON.toJSONString(result);
    }

    private String getMacInfo(WifiPosition position) {
        if (null == position || StringUtils.isEmpty(position.getMac())
                || StringUtils.isEmpty(position.getSignal()) || StringUtils.isEmpty(position.getSsid())) {
            return null;
        }
        StringBuilder macStr = new StringBuilder(position.getMac());
        macStr.append(",");
        macStr.append(position.getSignal());
        macStr.append(",");
        macStr.append(position.getSsid());
        return macStr.toString();
    }
}
